﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Windvoice.Album.Model;
using Windvoice.Util;
using System.Data;

namespace Windvoice.Album.DataAccess
{
    public class PhotoCommentDAL
    {
        private string ConnectionString = ConfigurationManager.ConnectionStrings["windvoice"].ToString();

        public int PhotoComment_Insert(PhotoCommentInfo comment)
        {
            string command = "INSERT INTO [PhotoComment] ([authorid],[author],[content],[time],[ip],[photoid],[reply],[replytime]) VALUES ({0},'{1}','{2}','{3}','{4}',{5},'{6}','{7}')";
            command = String.Format(command,comment.AuthorID,comment.Author,comment.Content,comment.Time,comment.IP,comment.PhotoID,comment.Reply,comment.ReplyTime);
            return AccessHelper.ExecuteNonQuery(ConnectionString,command);
        }
        //id, authorid, author, cotent, [time], ip, photoid, reply, replytime";

        public int PhotoComment_Delete(int CommentID)
        {
            string command = "DELETE FROM [PhotoComment] WHERE [id]=" + CommentID.ToString();
            return AccessHelper.ExecuteNonQuery(ConnectionString,command);
        }

        public int PhotoComment_Update(PhotoCommentInfo comment)
        {
            string command = "UPDATE [PhotoComment] SET [authorid]={0},[author]='{1}',[content]='{2}',[time]='{3}',[ip]='{4}',[photoid]={5},[reply]='{6}',[replytime]='{7}' WHERE [id]={8}";
            command = String.Format(command,comment.AuthorID,comment.Author,comment.Content,comment.Time,comment.IP,comment.PhotoID,comment.Reply,comment.ReplyTime,comment.ID);
            return AccessHelper.ExecuteNonQuery(ConnectionString,command);
        }

        public PhotoCommentInfo PhotoComment_Get(int CommentID)
        {
            string command = "SELECT [id],[authorid],[author],[cotent],[time],[ip],[photoid],[reply],[replytime] FROM [PhotoComment] WHERE [id]=" + CommentID.ToString();
            DataTable dt = AccessHelper.ExecuteDataSet(ConnectionString,command).Tables[0];
            if (dt.Rows.Count > 0) return new PhotoCommentInfo(dt.Rows[0]);
            else return null;
        }

        public DataTable PhotoComment_GetAll()
        {
            string command = "SELECT [id],[authorid],[author],[cotent],[time],[ip],[photoid],[reply],[replytime] FROM [PhotoComment]";
            return AccessHelper.ExecuteDataSet(ConnectionString,command).Tables[0];
        }

        public DataTable PhotoComment_GetList(int PhotoID)
        {
            string command = "SELECT [id],[authorid],[author],[cotent],[time],[ip],[photoid],[reply],[replytime] FROM [PhotoComment] WHERE [photoid]="+PhotoID.ToString();
            return AccessHelper.ExecuteDataSet(ConnectionString,command).Tables[0];
        }
    
    }
}
